/*
 * ConnectionFrame.java
 *
 * Created on August 24, 2011, 4:38 PM
 */

package datainsert.frames;

import datainsert.controlers.ListenersControler;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;

/**
 * Interface gráfica de conexão com a base de dados
 * @author  Yule Vaz
 */
public class ConnectionFrame extends javax.swing.JFrame {
    
    /**
     * Interface principal do aplicativo
     */
    private MainFrame mainFrame;
    /** Creates new form ConnectionFrame */
    /**
     * Constrói esta interface gráfica
     * @param mainFrame Interface principal do aplicativo
     */
    public ConnectionFrame( MainFrame mainFrame ) {
        initComponents();
        ConnectionActuator obs = new ConnectionActuator();
        ListenersControler.addButtonActionListener(obs, jbtConectar);
        ListenersControler.addButtonActionListener(obs, jbtCancelar);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.mainFrame = mainFrame;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jtxtIp = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jtxtPorta = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jtxtUsuario = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jpassSenha = new javax.swing.JPasswordField();
        jLabel6 = new javax.swing.JLabel();
        jtxtDB = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jbtConectar = new javax.swing.JButton();
        jbtCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Conexão");

        jLabel2.setText("IP");

        jtxtIp.setText("localhost");

        jLabel3.setText("Porta");

        jtxtPorta.setText("5432");

        jLabel4.setText("Usuário");

        jLabel5.setText("Senha");

        jLabel6.setText("Base de dados");

        jbtConectar.setText("Conectar");

        jbtCancelar.setText("Cancelar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(14, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel2)
                    .addComponent(jtxtIp, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                    .addComponent(jtxtPorta)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jtxtUsuario)
                    .addComponent(jLabel5)
                    .addComponent(jpassSenha)
                    .addComponent(jLabel6)
                    .addComponent(jtxtDB))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(167, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jbtConectar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtCancelar, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtxtIp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtxtPorta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtxtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpassSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtxtDB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtCancelar)
                    .addComponent(jbtConectar))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton jbtCancelar;
    private javax.swing.JButton jbtConectar;
    private javax.swing.JPasswordField jpassSenha;
    private javax.swing.JTextField jtxtDB;
    private javax.swing.JTextField jtxtIp;
    private javax.swing.JTextField jtxtPorta;
    private javax.swing.JTextField jtxtUsuario;
    // End of variables declaration//GEN-END:variables
    
    /**
     * Ouvinte desta interface gráfica. Detecta ações do usuário (eventos)
     */
    private class ConnectionActuator implements ActionListener {

        /**
         * Executa uma ação dependendo do evento acionado
         * @param e Evento acionado
         */
        public void actionPerformed(ActionEvent e) {
            
            if ( e.getSource() == jbtConectar ) {
                String ip = jtxtIp.getText();
                String port = jtxtPorta.getText();
                String usr = jtxtUsuario.getText();
                String pass = jpassSenha.getText();
                String db = jtxtDB.getText();
                mainFrame.configDB(ip, port, usr, pass, db);
                if ( mainFrame.isConnected() ) {
                    mainFrame.configDBCombo();
                    getFrame().dispose();
                }
            }
            
            if ( e.getSource() == jbtCancelar ) {
                getFrame().dispose();
            }
            
        }
        
    }
    
    /**
     * 
     * @return Esta interface 
     */
    private ConnectionFrame getFrame() {
        return this;
    }
    
}
